using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;

namespace AsyncTask
{
    public class Shape : MonoBehaviour
    {
        // /// <summary>
        // /// 1.使用协程实现
        // /// </summary>
        // /// <param name="duration">持续时间</param>
        // /// <returns></returns>
        // public IEnumerator RotateForSeconds(float duration)
        // {
        //     var end = Time.time + duration;
        //     while (Time.time < end)
        //     {
        //         transform.Rotate(new Vector3(1, 1) * Time.deltaTime * 150);
        //         yield return null;
        //     }
        // }        
        
        /// <summary>
        /// 使用异步
        /// </summary>
        /// <param name="duration">持续时间</param>
        public async void RotateForSecondsAsync(float duration)
        {
            var end = Time.time + duration;
            while (Time.time < end)
            {
                transform.Rotate(new Vector3(1, 1) * Time.deltaTime * 150);
                await Task.Yield();
            }
        }
        
        // 2.如果需要关注异步任务结束时间，只需要返回Task，Task本身就有执行状态信息
        public async Task RotateForSecondsAsyncTask(float duration)
        {
            var end = Time.time + duration;
            while (Time.time < end)
            {
                transform.Rotate(new Vector3(1, 1) * Time.deltaTime * 150);
                await Task.Yield();
            }
        }
    }
}
